﻿using UnityEngine;
using System.Collections;

public class CharViewerProjectile : MonoBehaviour {

	bool bMoveComplete;
	public GameObject hitFXPrefab;
	CharViewerPawn parent;
	CharViewerPawn target;
	public ProjectileInfo info;
	
	static float supportGuardTime = 0.3f;
	
	public float MoveToTarget(CharViewerPawn _parent , CharViewerPawn _target, Vector3 targetPos)
	{
		parent = _parent;
		target = _target;
		bMoveComplete=false;
		float dist = (targetPos - transform.position).magnitude;
		if(info.speed==0)
			info.speed=10;
		float duration = dist/info.speed;
		
		Debug.Log ("== projectile MoveToTarget  dist="+dist+"   speed="+info.speed+"    time="+duration);
		Hashtable tweenOption = new Hashtable();
		tweenOption.Add("ease", info.tweenType);
		tweenOption.Add("onComplete", "MoveComplete");
		tweenOption.Add("onCompleteTarget", this.gameObject);
		if(info.curveUpOffset==0)
			LeanTween.move(gameObject, targetPos, duration, tweenOption);
		else {
			Vector3 midUp = (transform.position + targetPos)/2 + Vector3.up * info.curveUpOffset;
			Vector3 [] pos = { transform.position, midUp,midUp, targetPos };
			LeanTween.move(gameObject, pos, duration, tweenOption);
		}
		return duration;
	}
	
	void MoveComplete()
	{
		if(bMoveComplete)
			return;
		bMoveComplete=true;

		Debug.Log("projectile MoveComplete");
		GetComponentInChildren<ParticleSystem>().Stop ();
		GetComponentInChildren<ParticleSystem>().loop = false;
		if(hitFXPrefab!=null) {
			GameObject go = Instantiate(hitFXPrefab) as GameObject;
			go.transform.parent = transform;
			BattleUtil.ClearTransformLocal(go.transform);
			go.SetActive(true);
			Destroy(go,2.0f);
		}
		//parent.parentPawn.isProjectileCasting=false;
		parent.projectile = null;
		target.OnDamage();
		Destroy(gameObject,3.0f);
	}
}
